//
//  HELPersonVC.m
//  HandheldELearn
//
//  Created by 陈武琦 on 16/7/11.
//  Copyright © 2016年 陈武琦. All rights reserved.
//

#import "HELPersonVC.h"
#import "HELPersonTableHeadView.h"
#import "HELPersonCollectionViewCell.h"
#import "HELPersonSettingVC.h"
#import "HELCompulsoryVC.h"
#import "HELExamVC.h"
#import "HELModifyPwdVC.h"
#import "HELMeetingInfo.h"
#import "HELStatisticVC.h"
#import "HELUserInfoM.h"
#import "UIButton+WebCache.h"
#import "HELSigninVC.h"
#import "CWQButtonWithBadge.h"

//#import "UIViewController+SQExtension.h"

@interface HELPersonVC ()<HELPersonTableHeadViewDelegate,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,CWQButtonWithBadgeDelegate>
@property (nonatomic,  strong) UICollectionView *collectionView;
@property (nonatomic,  strong) HELUserInfoM *userInfo;
@property (nonatomic,  strong) HELPersonTableHeadView *headView;
@property (nonatomic,  copy) NSDictionary *badgeDcit;
@property (nonatomic,  strong)CWQButtonWithBadge *messageBtn;
@end
static NSString *cellID = @"HELPersonCollectionViewCellID";
static NSString *headerId = @"HELPersonTableHeadViewID";

@implementation HELPersonVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor  whiteColor];
    [self leftAndRightItem];
    [self titleView:@"我的"];
    self.collectionView.backgroundColor = kAppBg;
    defineWeakSelf;
    [self.collectionView wsj_HeaderRefreshBlock:^{
        [weakSelf cwqGetWebData];
    }];

}

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:YES];
    [[HELAppManager sharedAppManger].appNav firstNav];
//    NSLog(@"self.view.frame:%@",NSStringFromCGRect(self.view.bounds));
    if ([[NSUserDefaults standardUserDefaults] boolForKey:@"personVCUPoffset"]) {
        self.collectionView.contentInset = UIEdgeInsetsMake(-64, 0, 0, 0);

    }
     [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"personVCUPoffset"];
     [[NSUserDefaults standardUserDefaults] synchronize];
    [self cwqGetWebData];
}


- (void)viewWillDisappear:(BOOL)animated {

   self.collectionView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}

- (void)leftAndRightItem {
    
    //设置
    UIBarButtonItem *settingItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"setting"] style:UIBarButtonItemStylePlain target:self action:@selector(settingItemPress)];
    
    [self.navigationItem setLeftBarButtonItem:settingItem];
    //消息
    _messageBtn = [[CWQButtonWithBadge alloc] initWithFrame:CGRectMake(0, 0, 40, 40) image:@"message"];
    _messageBtn.delegate = self;
    UIBarButtonItem *messageItem = [[UIBarButtonItem alloc] initWithCustomView:_messageBtn];
    
    [self.navigationItem setRightBarButtonItem:messageItem];
}

- (void)settingItemPress {
    
    HELPersonSettingVC *settingVC = [[HELPersonSettingVC alloc] init];
    settingVC.userInfoModel = self.userInfo;
    [self.navigationController pushViewController:settingVC animated:YES];

}


#pragma mark - ---------CWQButtonWithBadge------------
- (void)cwq_clickButton:(UIButton *)btn {
    HELMeetingInfo *meetingInfoVC = [HELMeetingInfo new];
    meetingInfoVC.title = @"会议信息";
    [self.navigationController pushViewController:meetingInfoVC animated:YES];
}

- (void)setBadgeDcit:(NSDictionary *)badgeDcit {
    _badgeDcit = badgeDcit;
    if ([badgeDcit[@"noticeUnRead"] integerValue] > 0) {
        self.messageBtn.badge.hidden = NO;
        self.messageBtn.badge.text = [NSString stringWithFormat:@"%@", badgeDcit[@"noticeUnRead"]];
    }else {
        self.messageBtn.badge.hidden = YES;
        self.messageBtn.badge.text = @"";
    }
    [self.collectionView reloadData];
    
}

- (UICollectionView *)collectionView{
    if (!_collectionView) {
       UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init]; // 自定义的布局对象
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight - 49) collectionViewLayout:layout];
        _collectionView.backgroundColor = kAppBg;
        _collectionView.dataSource = self;
        _collectionView.delegate = self;
        // 注册cell、sectionHeader、sectionFooter
        [_collectionView registerNib:[UINib nibWithNibName:@"HELPersonCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:cellID];
        [_collectionView registerNib:[UINib nibWithNibName:@"HELPersonTableHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerId];

        [self.view addSubview:_collectionView];
    }
    return _collectionView;
    
}

- (void)cwqGetWebData {

    helUserInfo
    defineWeakSelf;
    /*
     *获取角标
     */
     NSDictionary *param = @{@"userId":userInfo.userId};
    [[HELNetworkManager sharedMg] GET:helStatusBagueUrl parameters:param success:^(id responseObject) {
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            cwqHudRequestError(responseObject[@"exception"]);
        }else {
            if ([responseObject count] > 0) {
                weakSelf.badgeDcit = responseObject[0];
            }
        }
        [weakSelf.collectionView.header endRefreshing];
    } failure:^(NSString *err) {
        cwqHudRequestError(err);
        [weakSelf.collectionView.header endRefreshing];
    }];

}


#pragma mark UICollectionViewDataSource

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    CGFloat w = kScreenWidth;
    CGFloat h =44;
    return CGSizeMake(w, h);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(5, 5, 5, 5);
}


- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 5.f;
}



- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.493*kScreenHeight;
    return (CGSize){kScreenWidth,height > 280?height:280};
}



- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return 4;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    HELPersonCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    cell.badge.hidden = YES;
    switch (indexPath.row) {
        case 0:{
            [cell.leftIcon setImage:[UIImage imageNamed:@"signCourse"]];
            cell.title.text = @"签到课程";
        }
            break;
        case 1:{
            [cell.leftIcon setImage:[UIImage imageNamed:@"Group 12"]];
            cell.title.text = @"错题集";
        }
            break;
            
        case 2:{
            [cell.leftIcon setImage:[UIImage imageNamed:@"修改密码"]];
            cell.title.text = @"修改密码";
        }
            break;
            
        case 3:{
            [cell.leftIcon setImage:[UIImage imageNamed:@"个人信息"]];
            cell.title.text = @"个人信息";
        }
            break;
            
        case 4:{
            [cell.leftIcon setImage:[UIImage imageNamed:@"退出程序"]];
            cell.title.text = @"退出程序";
        }
            break;
            
            
        default:
            break;
    }

    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.row) {
        case 0:{
            HELSigninVC *signinVC = [HELSigninVC new];
            signinVC.title = @"签到课程";
            [self.navigationController pushViewController:signinVC animated:YES];
        }
            break;
        case 1:{
            HELStatisticVC *statisticVC = [HELStatisticVC new];
            statisticVC.title = @"错题集";
            [self.navigationController pushViewController:statisticVC animated:YES];
        }
            break;
            
        case 2:{
            HELModifyPwdVC *modifyPwdVC = [HELModifyPwdVC new];
            modifyPwdVC.title = @"修改密码";
            [self.navigationController pushViewController:modifyPwdVC animated:YES];
        }
            break;
            
        case 3:{
            HELPersonSettingVC *settingVC = [[HELPersonSettingVC alloc] init];
            settingVC.userInfoModel = self.userInfo;
            [self.navigationController pushViewController:settingVC animated:YES];
        }
            break;
            
        case 4:{
            //            [cell.leftIcon setImage:[UIImage imageNamed:@"退出程序"]];
            //            cell.title.text = @"退出程序";
        }
            break;
            
            
        default:
            break;
    }
    
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
 if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        HELPersonTableHeadView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:kind withReuseIdentifier:headerId forIndexPath:indexPath];
     [headerView sendSubviewToBack:headerView.rectangleImageView];
     [headerView sendSubviewToBack:headerView.backgroundImageView];
     headerView.compursol.layer.cornerRadius = 10;
     headerView.compursol.layer.masksToBounds = YES;
     headerView.exam.layer.cornerRadius = 10;
     headerView.exam.layer.masksToBounds = YES;
     headerView.headBtn.layer.cornerRadius = 30;
     headerView.headBtn.layer.borderWidth = 1.0;
     headerView.headBtn.layer.borderColor = [UIColor whiteColor].CGColor;
     headerView.headBtn.layer.masksToBounds = YES;
     
     
     HELUserInfoM *userInfo = [HELAppManager userInfo];
     [headerView.headBtn sd_setImageWithURL:[NSURL URLWithString:userInfo.photoShowMap.mini] forState:UIControlStateNormal placeholderImage:[UIImage imageNamed:@"headPhoto"]];
     headerView.name.text = userInfo.trueName;
     [headerView setBadge:self.badgeDcit];
     headerView.delegate = self;
     _headView = headerView;
        return headerView;
    }
     return nil;
}



#pragma mark-------------HELPersonTableHeadViewDelegate ----------------

- (void)headBtnClick {
    HELPersonSettingVC *settingVC = [[HELPersonSettingVC alloc] init];
    settingVC.userInfoModel = self.userInfo;
    [self.navigationController pushViewController:settingVC animated:YES];
}

- (void)headviewDidSelect:(NSInteger)index {

    switch (index) {
        case 1:{
            HELCompulsoryVC *compulsoryVC = [[HELCompulsoryVC alloc] initWithType:HELcourseTypeRequiredStatus];
            compulsoryVC.title = @"必修";
//            compulsoryVC
            [self.navigationController pushViewController:compulsoryVC animated:YES];
        }
            break;
        case 2:{
            HELCompulsoryVC *electVC = [[HELCompulsoryVC alloc] initWithType:HELcourseTypeCollectStatus];
            electVC.title = @"收藏";
            [self.navigationController pushViewController:electVC animated:YES];
        }
            
            break;
        case 3:{
            HELExamVC *examVC = [HELExamVC new];
//            examVC.title = @"我的考试";
            [self.navigationController pushViewController:examVC animated:YES];
        }
            
            break;
            
        default:
            break;
    }

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
